import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/simple_choice_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/department_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/work_mode_config_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_ticket/common/common_work_all/common_work_all_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:get/get.dart';

class FireJobApplicationController extends GetxController {
  //作业总页面controller
  final workAllPageController = Get.find<CommonWorkAllController>();

  //作业申请实体
  ExecuteTicketReceiptList executeTicketReceipt = ExecuteTicketReceiptList();

  //动火方式列表
  List<WorkModeConfigEntity> workModeConfigList = [];

  List<SimpleChoiceBean> fireJobLevel = []; //动火级别选择

  //选中的动火方式显示
  String modeConfigChecked = '';

  //作业负责人
  ExecuteTicketReceiptListOperatorLists principal = ExecuteTicketReceiptListOperatorLists();

  //监护人
  ExecuteTicketReceiptListOperatorLists guardian = ExecuteTicketReceiptListOperatorLists();

  @override
  void onInit() {
    super.onInit();
    //初始化页面列表
    initData();
  }

  ///初始化数据
  initData() async {
    //获取数据作业申请数据
    final res = await HttpUtil().generateGetRequest<ExecuteTicketReceiptList>(Interface.getTicketReceiptDetail,
        queryParameters: {'ticketReceiptId': workAllPageController.executeEntity.ticketReceiptId}).execute();
    res.onSuccess((result) {
      workAllPageController.executeEntity.ticketReceiptList = result.data;
    });
    //赋值
    executeTicketReceipt = workAllPageController.executeEntity.ticketReceiptList!;
    //逗号隔开的动火方式名字展示
    modeConfigChecked = executeTicketReceipt.workOtherDataDTO!.workModeConfigs!.map((e) => e.workMode).join(',');
    //查找已选中的人员
    for (var item in executeTicketReceipt.operatorLists!) {
      //作业负责人
      if (item.operatorType == 1) {
        principal = item;
      }
      //监护人
      if (item.operatorType == 2) {
        guardian = item;
      }
    }
    update();
    //获取动火方式列表
    _getWorkModeConfigEntity();
    //获取动火作业级别
    _getFireWorkType();
  }

  ///作业申请时间选择
  void showDatePicker() {
    CustomSelectFunction.selectDateTime(onCallBack: (String value) {
      executeTicketReceipt.applyDate = value;
      update();
    });
  }

  /// 获取动火方式列表
  void _getWorkModeConfigEntity() async {
    final res = await HttpUtil().generateGetRequest<List<WorkModeConfigEntity>>(Interface.getWorkModeConfigList,
        queryParameters: {'workTypeId': executeTicketReceipt.ticketBigTypeId}).execute();

    res.onSuccess((result) {
      workModeConfigList = result.data!;
      update();
    });
  }

  /// 获取动火作业级别
  void _getFireWorkType() {
    fireJobLevel.clear();
    final List<GetDictAllEntity> specialJobType = DictDataInfo.specialJobType;
    // 找到动火作业
    final fireDict = specialJobType.firstWhere((item) => '动火作业' == item.dictionaryLabel, orElse: GetDictAllEntity.new);
    // 如果有数据
    if (fireDict.id != null) {
      for (var item in fireDict.dictionaryDetails!) {
        fireJobLevel.add(SimpleChoiceBean(id: item.id!, name: item.dictionaryLabel!));
      }
    }
    // 判断之前是否已经选择
    if (executeTicketReceipt.ticketTypeId!.isNotEmpty) {
      try {
        // 找到选中的数据
        final fireDict = fireJobLevel.firstWhere((item) => executeTicketReceipt.ticketTypeId! == item.id);
        fireDict.setIsChecked(true);
      } catch (_) {}
    }
    update();
  }

  ///作业单位选取
  void applyUnitSelect() {
    Get.toNamed(AppRoutes.unitSelection, arguments: {
      'queryParameters': {'type': 2},
      'isRadio': true,
    })?.then((value) {
      //如果不为空则设置单位信息
      if (value != null) {
        value as List<DepartmentEntity>;
        executeTicketReceipt.workDepartmentName = value[0].getShowName();
        executeTicketReceipt.workDepartmentId = value[0].getId();
        executeTicketReceipt.workDepartmentType = value[0].workDepartmentType;
      }
      update();
    });
  }

  ///动火方式选择
  void showFireWay() {
    CustomSelectFunction.selectMultipleBottomSheet<WorkModeConfigEntity>(
      title: '动火方式选择',
      dataList: workModeConfigList,
      checkedList: executeTicketReceipt.workOtherDataDTO!.workModeConfigs!,
      onCallBack: (value) {
        executeTicketReceipt.workOtherDataDTO!.workModeConfigs = value;
        //逗号隔开
        modeConfigChecked = executeTicketReceipt.workOtherDataDTO!.workModeConfigs!.map((e) => e.workMode).join(',');
        update();
      },
    );
  }
}
